package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.BdSupplier;
import com.fowo.api.entity.Receivable;
import com.fowo.api.entity.StockOutProduct;
import com.fowo.api.entity.Supplier;
import com.fowo.api.model.receivable.ReceivableVo;
import com.fowo.api.model.receivable.product.ReceivableProductItemVo;
import com.fowo.api.model.stock.out.StockOutCreateReceivableAction;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.service.ReceivableProductService;
import com.fowo.api.service.ReceivableService;
import com.fowo.api.service.impl.*;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Component
public class stockOutCreateReceivable implements StockOutCreateReceivableAction {

    @Resource
    private StockOutServiceImpl stockOutServiceImpl;
    @Resource
    private StockOutProductServiceImpl stockOutProductService;
    @Resource
    private ReceivableServiceImpl receivableService;
    @Resource
    private ReceivableProductServiceImpl receivableProductService;
    @Resource
    private SupplierServiceImpl supplierService;

    @Resource
    private BdSupplierServiceImpl bdSupplierService;

    @Override
    public void run(Long id) throws Exception {
        StockOutVo voById = stockOutServiceImpl.getVoById(id);
        Assert.notNull(voById,"不存在！");
        Assert.isTrue(voById.getStatus().equals("CS02"),"未审核");
        List<StockOutProduct> list = stockOutProductService.list(Wrappers.<StockOutProduct>query().lambda().eq(StockOutProduct::getStockOutId, id));
        List<Receivable> list1 = receivableService.list(Wrappers.<Receivable>query().lambda().eq(Receivable::getRemark, voById.getRemark()));
        Assert.isTrue(list1.size()==0,"已生成应收单,不能重复生成");
        addReceivable(voById,list);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addReceivable(StockOutVo voById, List<StockOutProduct> list) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        Supplier one1 = supplierService.getOne(Wrappers.<Supplier>query().lambda().eq(Supplier::getSupplierCode, "03001"));
        ReceivableVo model=new ReceivableVo();
        model.setCloseOrg(voById.getNeedOrg());
        model.setBusinessDate(voById.getOutTime());
        model.setCustomer(one1!=null?one1.getId():null);
        model.setStatus("0");
        model.setProductType(voById.getProductType());
        model.setPushStatus("0");
        if(voById.getRemark()==null){
            model.setRemark("出库单号:"+voById.getOrderSn());
        }else{
            model.setRemark(voById.getRemark());
        }
        model.setPurchaseOrder(getOrder(voById.getRemark()));
        model.setWarehouseSn(voById.getOrderSn());
        model.setPreparedBy(currentUser.getUserId());
        List<ReceivableProductItemVo> products=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            StockOutProduct stockOutProduct = list.get(i);
            ReceivableProductItemVo receivableProductItem=new ReceivableProductItemVo();
            receivableProductItem.setNum(stockOutProduct.getQuantity());
            receivableProductItem.setProductCode(stockOutProduct.getProductId());
            receivableProductItem.setProductSku(stockOutProduct.getSku());
            receivableProductItem.setProductName(stockOutProduct.getProductName());
            receivableProductItem.setPrice(stockOutProduct.getPrice());
            receivableProductItem.setAmount(stockOutProduct.getSubtotal());
            receivableProductItem.setProductName(stockOutProduct.getProductName());
            products.add(receivableProductItem);
        }
        model.setProducts(products);
        receivableService.create(model);
    }

    String getOrder(String orderSn){
        String po=null;
        if(orderSn!=null){
            if(orderSn.indexOf("PO")!=-1){
                try{
                    po=orderSn.substring(orderSn.indexOf("PO"),orderSn.indexOf("PO")+11);
                }catch (Exception e){

                }
            }
        }
        return  po;
    }

}
